package org.bdware.doip.endpoint.doipServer;

import com.google.gson.Gson;
import org.apache.log4j.Logger;

import java.net.URI;

public abstract class NettyDoipListener implements DoipListener{

    static Logger logger = Logger.getLogger(NettyDoipListener.class);
    NettyServerHandler handler;

    public static NettyDoipListener CreateDoipListener(DoipListenerInfo listenerInfo) {
        try {
            URI uri = new URI(listenerInfo.url);
//            logger.debug("[URI Parse]scheme:" + uri.getScheme() + "host: " + uri.getHost() + "  port: " + uri.getPort());
            switch (uri.getScheme()) {
                case "tcp":
                case "TCP":
                    return new NettyTCPDoipListener(uri.getPort());
                case "udp":
                case "UDP":
                    return new NettyUDPDoipListener(uri.getPort());
                case "tls":
                case "TLS":
                    return new NettyTLSDoipListener(uri.getPort());
                case "ws":
                case "WS":
                    return new NettyWSDoipListener(uri.getPort(),uri.getPath());
                default:
                    logger.error("[Create repository error] Unsupported Listener: " + new Gson().toJson(listenerInfo));
                    return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public void setRequestHandler(DoipRequestHandler handler) {
        this.handler = new NettyServerHandler(handler);
    }

    public abstract void start();
    public abstract void stop();

}

